using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using VotingSystem.Models;

namespace VotingSystem.Data
{
    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }

        public DbSet<Vote> Votes { get; set; }
        public DbSet<VoteOption> VoteOptions { get; set; }
        public DbSet<VoteRecord> VoteRecords { get; set; }
        public DbSet<Survey> Surveys { get; set; }
        public DbSet<SurveyQuestion> SurveyQuestions { get; set; }
        public DbSet<QuestionOption> QuestionOptions { get; set; }
        public DbSet<SurveyResponse> SurveyResponses { get; set; }
        public DbSet<QuestionResponse> QuestionResponses { get; set; }
        public DbSet<OptionResponse> OptionResponses { get; set; }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);

            // 投票相关配置
            builder.Entity<Vote>()
                .HasOne(v => v.Creator)
                .WithMany(u => u.CreatedVotes)
                .HasForeignKey(v => v.CreatorId)
                .OnDelete(DeleteBehavior.Restrict);

            builder.Entity<VoteRecord>()
                .HasOne(r => r.User)
                .WithMany()
                .HasForeignKey(r => r.UserId)
                .OnDelete(DeleteBehavior.Restrict);

            // 问卷相关配置
            builder.Entity<Survey>(entity =>
            {
                entity.HasOne(s => s.Creator)
                    .WithMany()
                    .HasForeignKey(s => s.CreatorId)
                    .OnDelete(DeleteBehavior.Restrict);

                entity.Property(s => s.IsPublic)
                    .HasDefaultValue(true);

                entity.Property(s => s.IsActive)
                    .HasDefaultValue(true);

                entity.Property(s => s.RequireLogin)
                    .HasDefaultValue(true);
            });

            builder.Entity<SurveyQuestion>(entity =>
            {
                entity.HasOne(q => q.Survey)
                    .WithMany(s => s.Questions)
                    .HasForeignKey(q => q.SurveyId)
                    .OnDelete(DeleteBehavior.Cascade);

                entity.Property(q => q.IsRequired)
                    .HasDefaultValue(true);
            });

            builder.Entity<QuestionOption>(entity =>
            {
                entity.HasOne(o => o.Question)
                    .WithMany(q => q.Options)
                    .HasForeignKey(o => o.QuestionId)
                    .OnDelete(DeleteBehavior.Cascade);
            });

            builder.Entity<SurveyResponse>(entity =>
            {
                entity.HasOne(r => r.Survey)
                    .WithMany(s => s.Responses)
                    .HasForeignKey(r => r.SurveyId)
                    .OnDelete(DeleteBehavior.Cascade);

                entity.HasOne(r => r.Respondent)
                    .WithMany()
                    .HasForeignKey(r => r.RespondentId)
                    .OnDelete(DeleteBehavior.Restrict);
            });

            builder.Entity<QuestionResponse>(entity =>
            {
                entity.HasOne(r => r.SurveyResponse)
                    .WithMany(sr => sr.QuestionResponses)
                    .HasForeignKey(r => r.SurveyResponseId)
                    .OnDelete(DeleteBehavior.Cascade);

                entity.HasOne(r => r.Question)
                    .WithMany(q => q.Responses)
                    .HasForeignKey(r => r.QuestionId)
                    .OnDelete(DeleteBehavior.Restrict);
            });

            builder.Entity<OptionResponse>(entity =>
            {
                entity.HasOne(r => r.QuestionResponse)
                    .WithMany(qr => qr.SelectedOptions)
                    .HasForeignKey(r => r.QuestionResponseId)
                    .OnDelete(DeleteBehavior.Cascade);

                entity.HasOne(r => r.Option)
                    .WithMany(o => o.OptionResponses)
                    .HasForeignKey(r => r.OptionId)
                    .OnDelete(DeleteBehavior.Restrict);
            });
        }
    }
} 